﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace QuickAppLibrary
{
    /// <summary>
    /// Provides A Collection for the PriorEmployer Class
    /// </summary>
    public class PriorEmployerCollection : ICollection<PriorEmployer>
    {
        //Class Varibles
        //Inner Collection
        List<PriorEmployer> innerCol;
        private bool isRO = false;

        /// <summary>
        /// Gets Enumerator, provided by the PriorEmployerEnumerator Class
        /// </summary>
        /// <returns>PriorEmployerEnumerator</returns>
        public IEnumerator<PriorEmployer> GetEnumerator()
        {
            return new PriorEmployerEnumerator(this);
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return new PriorEmployerEnumerator(this);
        }

        /// <summary>
        /// Read-Only property to determine if collection is read-only; set to false
        /// </summary>
        public bool IsReadOnly
        {
            get { return isRO; }
        }

        /// <summary>
        /// Constructor for collection
        /// </summary>
        public PriorEmployerCollection()
        {
            innerCol = new List<PriorEmployer>();
        }
        
        /// <summary>
        /// Adds a PriorEmployer object to the collection
        /// </summary>
        /// <param name="priorEmployer"></param>
        public void Add(PriorEmployer priorEmployer)
        {
            innerCol.Add(priorEmployer);
        }

        /// <summary>
        /// Determines if the collection contains the current item
        /// </summary>
        /// <param name="priorEmployer"></param>
        /// <returns></returns>
        public bool Contains(PriorEmployer priorEmployer)
        {
            bool found = false;

            foreach (PriorEmployer pm in innerCol)
            {
                if (pm.Equals(priorEmployer))
                {
                    found = true;
                }
            }

            return found;
        }

        /// <summary>
        /// Clears the collection
        /// </summary>
        public void Clear()
        {
            innerCol.Clear();
        }

        /// <summary>
        /// Removes a specific PriorEmployer object from the collection. Returns true if found.
        /// </summary>
        /// <param name="priorEmployer"></param>
        /// <returns>Boolean</returns>
        public bool Remove(PriorEmployer priorEmployer)
        {
            bool result = false;

            //Iterate over collection
            for (int i = 0; i < innerCol.Count; i++)
            {
                PriorEmployer tempEmp = (PriorEmployer)innerCol[i];

                if (tempEmp.Equals(priorEmployer))
                {
                    innerCol.RemoveAt(i);
                    result = true;
                    break;
                }
            }
            return result;
        }

        /// <summary>
        /// Indexer for the collection
        /// </summary>
        /// <param name="index"></param>
        public PriorEmployer this[int index]
        {
            get { return (PriorEmployer)innerCol[index]; }
            set { innerCol[index] = value; }
        }

        /// <summary>
        /// Gets the current amount of objects in the collection
        /// </summary>
        public int Count
        {
            get
            {
                return innerCol.Count;
            }
        }

        /// <summary>
        /// Not Implemented
        /// </summary>
        /// <param name="empArray"></param>
        /// <param name="arrayIndex"></param>
        public void CopyTo(PriorEmployer[] empArray, int arrayIndex)
        {
            //Blank
            throw new NotImplementedException("Not Implemented");
        }

        //Specific Class Methods
        /// <summary>
        /// FileStream service that serializes the collection to an XML file
        /// </summary>
        /// <param name="path"></param>
        public void SerializeToFile(string path)
        {
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                XmlSerializer xSer = new XmlSerializer(innerCol.GetType());
                xSer.Serialize(fs, innerCol);
            }

        }

        /// <summary>
        /// FileStream service that deserializes the collection from an XML file
        /// </summary>
        /// <param name="path"></param>
        public void DeserizlizeFromFile(string path)
        {
            if (File.Exists(path))
            {
                using (FileStream fs = new FileStream(path, FileMode.Open))
                {
                    XmlSerializer xSer = new XmlSerializer(innerCol.GetType());
                    this.innerCol = (List<PriorEmployer>)xSer.Deserialize(fs);
                }
            }
        }
    }
}
